package com.yiwei.ydd.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.ydd.R;
import com.yiwei.ydd.activity.WebViewActivity;
import com.yiwei.ydd.api.model.ArticleExplainModel;
import com.yiwei.ydd.api.model.IndexModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class AllTipsAdapter extends ItemAdapter<ArticleExplainModel.DatasBean, AllTipsAdapter.DefaultHolder> {


    private Context context;

    public AllTipsAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_all_tips, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        ArticleExplainModel.DatasBean item = getItem(position);
        holder.txtTitle.setText(item.title);
        holder.txtDesc.setText(item.desc);
        Glide.with(context).load(item.icon).into(holder.imgIcon);

        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.type.equals("2")) {
                    if (onChooseListener != null) {
                        onChooseListener.onClick();
                    }
                } else {
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("title", item.title);
                    intent.putExtra("url", item.url);
                    context.startActivity(intent);
                    ((Activity) context).overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                }


            }
        });
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_desc)
        TextView txtDesc;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick();
    }
}
